# -*- coding: utf-8 -*-
'''
@文件    :menu.py
@说明    :菜单管理
@时间    :2023/12/19 19:57:04
@作者    :浪晋
@版本    :1.0
'''

from app.admin import auth
from flask import request,g
from utils.requetFunc import RequestParser,pageConvert
from utils.responseFunc import resultMsg
from utils.commomFunc import build_tree
from json import loads,dumps


@auth.route("/menu/list",methods=["post"])
def menuList():
    """
    @功能    :菜单列表
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    title = body.get("title")
    parse = RequestParser()
    parse.add_argument(value=title,name="菜单名称",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "select id,name,parentId,sort,status,meta,component,redirect,path,DATE_FORMAT(createTime,'%Y-%m-%d %H:%i:%s') createTime from tb_system_menu where status != 0"
    if title not in (None,""):
        sql = sql + " and JSON_EXTRACT(meta, '$.title') = '{}'".format(title)
    sql = sql + " order by sort,id;"
    result = []
    for menu in db.fetchall(sql):
        menu.update(meta=loads(menu["meta"]) )
        result.append(menu)
    result = build_tree(result)
    return resultMsg(data=result)



@auth.route("/menu/create",methods=["post"])
def menuAdd():
    """
    @功能    :新增菜单
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    name = body.get("name")
    parentId = body.get("parentId",0) or 0
    path = body.get("path")
    component = body.get("component")
    meta = body.get("meta")
    redirect = body.get("redirect")
    parse = RequestParser()
    parse.add_argument(value=name,name="别名",required=True,remark="")
    parse.add_argument(value=parentId,name="上级ID",required=True,remark="必填且只能是整数",type=(int))
    parse.add_argument(value=path,name="路由",required=True,remark="",type=(str))
    parse.add_argument(value=component,name="组件",remark="")
    parse.add_argument(value=meta,name="元信息",remark="",type=(dict))
    parse.add_argument(value=redirect,name="重定向",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "insert into tb_system_menu (name,parentId,path,component,meta,redirect) values (%s,%s,%s,%s,%s,%s);"
    db.execute(sql,(name,parentId,path,component,dumps(meta),redirect))
    db.commit()
    return resultMsg()



@auth.route("/menu/edit",methods=["post"])
def menuEdit():
    """
    @功能    :修改菜单
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    mid = body.get("id")
    name = body.get("name")
    parentId = body.get("parentId",0)  or 0
    path = body.get("path")
    component = body.get("component")
    meta = body.get("meta")
    redirect = body.get("redirect")
    parse = RequestParser()
    parse.add_argument(value=mid,name="菜单Id",required=True,remark="")
    parse.add_argument(value=name,name="别名",required=True,remark="")
    parse.add_argument(value=parentId,name="上级ID",required=True,remark="")
    parse.add_argument(value=path,name="路由",required=True,remark="")
    parse.add_argument(value=component,name="组件",remark="")
    parse.add_argument(value=meta,name="元信息",remark="",type=(dict))
    parse.add_argument(value=redirect,name="重定向",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_menu set name=%s,parentId=%s,path=%s,component=%s,meta=%s,redirect=%s where id = %s;"
    db.execute(sql,(name,parentId,path,component,dumps(meta),redirect,mid))
    db.commit()
    return resultMsg()



@auth.route("/menu/del",methods=["post"])
def menuDel():
    """
    @功能    :删除菜单
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    idList = body.get("idList")
    parse = RequestParser()
    parse.add_argument(value=idList,name="菜单Id列表",required=True,remark="",type=(list,tuple),lenght=(1,1000))
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_menu set status=0 where id = %s;"
    db.executemany(sql,idList)
    db.commit()
    return resultMsg()
